package car.aistar.controller;

import car.aistar.dto.Result;
import car.aistar.entity.*;
import car.aistar.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;


/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2020-09-30 20:14:11
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends HttpServlet {
    /**
     * 服务对象
     */
    @Autowired
    private UserDetailService userDetailService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private CarDetailService carDetailService;
    @Autowired
    private CarownerService carownerService;
    @Autowired
    private UserService userService;

    /**
     *@Author 许华钢
     *@Description 管理员页面实现新增车主
     *@Date 2:47 2020/10/8
     *@Param
     *@return
     **/
    @ResponseBody
    @RequestMapping(value="/addOwner",method= RequestMethod.POST)
    public void addOwner(Model model, @RequestBody Carowner carowner, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("插入之前的数据情况："+carowner);

        Carowner ownerCheck = new Carowner();
        ownerCheck.setOIdcard(carowner.getOIdcard());
        if (carownerService.queryAll(ownerCheck).size()!=0){
            req.getSession().setAttribute("insertOwner",0);
            req.getSession().setAttribute("remind",1);
            return;
        }else{
            // 销毁session作用域
            req.getSession().setAttribute("remind",0);
        }

        carownerService.insert(carowner);
        req.getSession().setAttribute("insertOwner",1);
        Carowner owner = new Carowner();
        owner.setCId(carowner.getCId());
        owner.setObank(carowner.getObank());
        owner.setOIdcard(carowner.getOIdcard());
        owner.setOname(carowner.getOname());
//        System.out.println("插入数据2"+carownerService.queryAll(owner));
//        System.out.println("插入数据1"+carownerService.queryAll(owner).get(0));
        req.getSession().setAttribute("ownerID",carownerService.queryAll(owner).get(0).getId());

    }

    /**
     *@Author 许华钢
     *@Description 管理员页面对车辆管理中汽车详情表分页展示
     *@Date 15:47 2020/10/7
     *@Param
     *@return
     **/
    @RequestMapping("/showDescCar")
    public String car(Model model,String search,String pageNow,String pageSize){
        pageNow = pageNow==null?"1":pageNow;
        search = search==null?"":search;
        if(pageSize == null  || "-1".equals(pageSize)){
            pageSize = "2";
        }
        PageInfo<CarDetail> carDetailPageInfo = carDetailService.findByPageCarDetail(search,Integer.valueOf(pageNow),Integer.valueOf(pageSize));

        model.addAttribute("search",search);
        model.addAttribute("page",carDetailPageInfo);
        return "adCenter/newCar";
    }

    /**
     *@Author 许华钢
     *@Description 管理员页面实现新增车辆上架
     *@Date 16:47 2020/10/7
     *@Param
     *@return
     **/
    @ResponseBody
    @RequestMapping(value="/addCar",method= RequestMethod.POST)
    public void addCar(Model model, @RequestBody CarDetail carDetail, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String car = req.getParameter("json");
//        System.out.println("插入之前的数据情况："+carDetail);
        CarDetail car = new CarDetail();
        car.setCarCarnumber(carDetail.getCarCarnumber());
//        System.out.println("数据"+carDetailService.queryAll(car));
        if (carDetailService.queryAll(car).size()!=0){
            req.getSession().setAttribute("insertStatus",0);
            req.getSession().setAttribute("remind",1);
            return;
        }else {
            // 销毁session作用域
            req.getSession().setAttribute("remind",0);
        }
        carDetail.setCPhoto("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601394199236&di=24f81e6ea4470bf66afa49d35c5f0a68&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fe1014181f17b99c817cdb7aa0862b34c2a16fbd96d10f-9EiChL_fw658");
        carDetail.setCStatus(1);
        carDetail.setCarCount(20);
        carDetailService.insert(carDetail);
        req.getSession().setAttribute("insertStatus",1);
//        System.out.println("插入之后的数据情况："+carDetailService.queryAll(carDetail).get(0));
        req.getSession().setAttribute("carID",carDetailService.queryAll(car).get(0).getId());

    }

    /**
     *@Author 许华钢
     *@Description 管理员页面车辆管理表实现单击删除
     *@Date 16:47 2020/10/8
     *@Param
     *@return
     **/
    @RequestMapping("/delCarDetail")
    public @ResponseBody
    Result delCarDetail(Model model, String id){
        carDetailService.deleteById(Long.parseLong(id));
        return new Result("200","ok",null);
    }

    /**
     * @Author 许华钢
     * @Description 管理员页面车辆管理表实现批量删除
     * @param model
     * @param cid
     * @return
     */
    @RequestMapping("/delAllCarDetail")
    public @ResponseBody
    Result delAllCarDetail(Model model, String cid){
        String[] cids=cid.split(":");
        if(null!=cids && cids.length>0){
            for (String id : cids) {
                carDetailService.deleteById(Long.valueOf(id));
            }
        }
        return new Result("200","ok",null);
    }


    /**
     *
     * @Author 许华钢
     * @Descript 根据车辆管理表（管理员页面）更改车详情表中的相应字段
     * @param carDetail
     * @return
     */
    @RequestMapping("/carDetailsUpdate")
    public @ResponseBody
    Result carDetailsupdate(CarDetail carDetail){

        CarDetail carDetailUpdate = carDetailService.queryById(carDetail.getId());


        if (carDetailUpdate.getCOprice()!=null) {
            // 更新：根据车辆管理表（管理员页面）更改车详情表中的车辆售价字段
            carDetailUpdate.setCOprice(carDetail.getCOprice());
        }
        if(carDetailUpdate.getSalId()!=null){
            // 更新：根据车辆管理表（管理员页面）更改车详情表中的销售员id字段
            System.err.println("==");//err
            carDetailUpdate.setSalId(carDetail.getSalId());
        }
        carDetailService.update(carDetailUpdate);

        return new Result("200","ok",null);
    }


    /**
     *@Author 肖相荣
     *@Description //TODO 管理员页面对用户表全局搜索展示
     *@Date 15:47 2020/10/7
     *@Param
     *@return
     **/
    @RequestMapping("/one")
    public String auser(Model model,String search,String pageNow,String pageSize){

        pageNow = pageNow==null?"1":pageNow;
        search = search==null?"":search;
        if(pageSize == null  || "-1".equals(pageSize)){
            pageSize = "2";
        }
        PageInfo<UserDetail> userDetailPageInfo = userDetailService.findByPageUser(search,Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        model.addAttribute("search",search);
        model.addAttribute("page",userDetailPageInfo);
        return "adCenter/Auser";
    }


    /**
    *@Author 肖相荣
    *@Description //TODO 管理员页面对用户表的单个异步删除
    *@Date 15:47 2020/10/7
    *@Param
    *@return
    **/
    @RequestMapping("/deluser")
    public @ResponseBody
    Result del(Model model, String id){
        userDetailService.deleteById(Long.parseLong(id));
        return new Result("200","ok",null);
    }

    /**
    *@Author 肖相荣
    *@Description //TODO 管理员页面对用户表的批量删除
    *@Date 15:41 2020/10/7
    *@Param [model, sid]
    *@return car.aistar.dto.Result
    **/
    @RequestMapping("/delAlluser")
    public @ResponseBody
    Result delalluser(Model model, String sid){
        String[] sids=sid.split(":");
        if(null!=sids && sids.length>0){

            for (String id : sids) {
                userDetailService.deleteById(Long.valueOf(id));
            }
        }
        return new Result("200","ok",null);
    }
    /**
     *@Author 肖相荣
     *@Description //TODO 用户
     *@Date 20:42 2020/10/7
     *@Param
     *@return
     **/
    @RequestMapping("/userupdate")
    public @ResponseBody
    Result userupdate(UserDetail userDetail){

        UserDetail userDetail1 = userDetailService.queryById(userDetail.getId());

        if (userDetail.getUname()!=null) {

            userDetail1.setUname(userDetail.getUname());

        }else if(userDetail.getUpwd()!=null){
            userDetail1.setUpwd(userDetail.getUpwd());
        }
        else  if (userDetail.getUphone()!=null){

            userDetail1.setUphone(userDetail.getUphone());
        }
        else  if (userDetail.getUtime()!=null){
            userDetail1.setUtime(userDetail.getUtime());
        }
        else  if (userDetail.getAddress()!=null){
            userDetail1.setAddress(userDetail.getAddress());
        }
        User user = new User();
        user.setUId(userDetail1.getUId());
        user.setUname(userDetail1.getUname());
        user.setPhone(userDetail1.getUphone());
        user.setPwd(userDetail1.getUpwd());
        userService.update(user);
        userDetailService.update(userDetail1);

        return new Result("200","ok",null);
    }
    /**
     *@Author 肖相荣
     *@Description //TODO 管理员页面对销售人员表全局搜索展示
     *@Date 15:47 2020/10/7
     *@Param
     *@return
     **/
    @RequestMapping("/two")
    public String seller(Model model,String search,String pageNow,String pageSize){

        pageNow = pageNow==null?"1":pageNow;
        search = search==null?"":search;
        if(pageSize == null  || "-1".equals(pageSize)){
            pageSize = "2";
        }
        PageInfo<Seller> sellerPageInfo = sellerService.findByPageSeller(search,Integer.valueOf(pageNow),Integer.valueOf(pageSize));
        model.addAttribute("search",search);
        model.addAttribute("page",sellerPageInfo);
        return "adCenter/Aseller";
    }


    /**
     *@Author 肖相荣
     *@Description //TODO 管理员页面对销售人员的单个异步删除
     *@Date 15:47 2020/10/7
     *@Param
     *@return
     **/
    @RequestMapping("/delseller")
    public @ResponseBody
    Result delseller(Model model, String id){
        sellerService.deleteById(Long.parseLong(id));
        return new Result("200","ok",null);
    }

    /**
     *@Author 肖相荣
     *@Description //TODO 管理员页面对销售人员的批量删除
     *@Date 15:41 2020/10/7
     *@Param [model, sid]
     *@return car.aistar.dto.Result
     **/
    @RequestMapping("/delAllseller")
    public @ResponseBody
    Result delAllseller(Model model, String sid){
        String[] sids=sid.split(":");
        if(null!=sids && sids.length>0){
            for (String id : sids) {
                sellerService.deleteById(Long.valueOf(id));
            }
        }
        return new Result("200","ok",null);
    }

    /**
    *@Author 肖相荣
    *@Description //TODO 销售人员的更新
    *@Date 20:42 2020/10/7
    *@Param
    *@return
    **/
    @RequestMapping("/update")
    public @ResponseBody
    Result sellerupdate(Seller seller){
        System.out.println(seller);
        Seller seller1 = sellerService.queryById(seller.getSid());
        System.out.println(seller1);
        if (seller.getUname()!=null) {
            System.out.println(seller.getUname());
            seller1.setUname(seller.getUname());

        }else if(seller.getAccount()!=null){
            seller1.setAccount(seller.getAccount());
        }
        else  if (seller.getPwd()!=null){
            seller1.setPwd(seller.getPwd());
        }
        else  if (seller.getResDate()!=null){
            seller1.setResDate(seller.getResDate());
        }
        else  if (seller.getPhone()!=null){
            seller1.setPhone(seller.getPhone());
        }
        else  if (seller.getSellTotal()!=null){
            seller1.setSellTotal(seller.getSellTotal());
        }
        sellerService.update(seller1);

        return new Result("200","ok",null);
    }









}